<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="../js/vue.min.js"></script>
    <script src="../js/axios.min.js"></script>
    <title>Document</title>
  </head>

  <body>
    <div id="app">
      <button type="button" @click="testMyAxios">点击</button>
      <button type="button" @click="testConcurrentRequest">并发请求按钮</button>
      <button type="button" @click="getMusicDetail">接口返回的数据</button>
      <button type="button" @click="method1">箭头函数按钮</button>
      {{musicDetail}}
      <table>
        <tr>
          <td>{{song.name}}</td>
          <td></td>
          <td></td>
        </tr>
      </table>
    </div>
    <script>
      var vue = new Vue({
        el: "#app",
        data: {
          song: {},
          musicDetail: [],
        },
        methods: {
          test1: function () {
            // 发送异步请求
            axios
              .get("http://localhost:9999/music/detail", {
                params: {
                  id: "25640392",
                },
              })
              .then(function (res) {
                alert(res.data.code);
                vue.songs = res.data.songs;
              });
          },
          testPost: function () {
            axios
              .post("http://localhost:9999/music/search", {
                s: "阿刁",
              })
              .then(function (res) {
                console.log(res);
              });
          },
          testMyAxios: function () {
            axios({
              method: "post",
              url: "http://localhost:9999/music/search",
              params: {
                // 设置请求行传值
                s: "成都",
                limit: 15,
              },
              headers: {
                // 设置请求头
              },
              data: {
                // 设置请求体，只限于post/put
              },
            }).then(function (res) {
              console.log(res);
            });
          },
          // 请求1
          listMusics: function () {
            return axios.post("http://localhost:9999/music/search?s=成都");
          },
          // 请求2
          getMusicDetail: function () {
            return axios.get("http://localhost:9999/music/detail?id=25640392");
          },
          // 并发请求
          testConcurrentRequest: function () {
            axios.all([vue.listMusics(), vue.getMusicDetail()]).then(
              axios.spread(function (res1, res2) {
                console.log(res1);
                console.log(res2);
              })
            );
          },
          // 箭头函数
          method1: function () {
            axios
              .get("http://localhost:9999/music/detail?id=25640392")
              .then((res) => {
                if (res.data.code == 200) {
                  this.song = res.data.songs[0];
                }
              });
          },
        },
      });
    </script>
  </body>
</html>
